package com.chance.cc.crawler.prod.command.job.domain.news.tenxun.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.tenxun.TXCommonScript;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName tx
 * @Description TODO
 * @Author songding
 * @Date 2021/10/22 10:30
 * @Version 1.0
 **/
public class TencentCrarCrawlerScheduleJob extends TXCommonScript {
    public static String site = "carinfo";//站点-
    public static void main(String[] args) {
        publishTengXunCarSeriesArticleCrawlerScheduleJobInfo();
    }//-

    public static CrawlerJob publishTengXunCarSeriesArticleCrawlerScheduleJobInfo(){//发布-
        CrawlerJob crawlerJob = ArticleScheduler();//采集-

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();//定时采集作业
        crawlerScheduleJob.setDomain(domain);//设置域
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));//设置采集信息
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());//工作标签
        crawlerScheduleJob.setNote("腾讯车系车型采集");//采集提示-
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());//生成爬虫key
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);//发布标签命令添加或修改脚本调度工作
        System.out.println("发布作业：" + page.getRawText());//打印文本
        return crawlerJob;//返回工作爬虫
    }
    public static CrawlerJob ArticleScheduler() {
        String START_URL = "https://auto.qq.com/car_public/index.shtml?serial_id=%s";
        Integer serial_id = 184;
        String start_url = String.format(START_URL,serial_id);
        String DOMAIN = "tenxun";
        String site_biz = "realtime";

        Long nowTime = System.currentTimeMillis();
        CrawlerRequestRecord startRecord = CrawlerRequestRecord.builder()
                .startPageRequest(DOMAIN, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(DOMAIN)
                .recordKey(start_url)
                .httpUrl(start_url)
                .releaseTime(nowTime)
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.memoryFilterKeyInfo(DOMAIN))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null))
                .build();
        startRecord.tagsCreator().bizTags().addDomain(DOMAIN);
        startRecord.tagsCreator().bizTags().addSite(site);
        startRecord.tagsCreator().bizTags().addSiteBiz(site_biz);

        //请求队列名
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,site,site_biz,"queue");//请求队列名

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(3)//3个线程
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//tenxun-carinfo-realtime-realtime
                        StringUtils.joinWith("-", site,site_biz, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))//请求队列
                //.queueResultPipeline("redis",crawlerQueueConfig)//链接保存在redis中-
                //.fileResultPipeline(null,"/data/chance_crawler_runner/logs/node/tianya.log",false)
                .kafkaResultPipeline(null, kafkaTopic, null)//数据保存在kafka中-
                .requestRecord(startRecord)
              //.supportRecord(initNewsCrawlerRecord)
                //.supportRecord(syncCrawlerRecord)
                .build();
        return crawlerJob;




    }





}
